<?php
include("config.php");

function getPG($query){
	$dbconn = pg_connect($GLOBALS['pghost'])
    	or die('Could not connect: ' . pg_last_error());

	$result = pg_query($query) or die('Query failed: ' . pg_last_error());
	$ret = array();
	$index = 0;

	while($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
		$ret[$index] = $line;
		$index = $index+1;
	}

	pg_free_result($result);
	pg_close($dbconn);
	
	return $ret;
}

function updatePG($query){
	$dbconn = pg_connect($GLOBALS['pghost'])
		or die('Could not connect: ' . pg_last_error());
		
	$result = pg_exec($dbconn,$query);

	pg_close($dbconn);
	
	return $result;
}

function login(){
	if(isset($_SESSION['edoc_user'])){
		$user = $_SESSION['edoc_user'];
		$pass = $_SESSION['edoc_pass'];
	}else{
		$user = $_POST['uname'];
		$pass = moo_crypt($_POST['pword']);
	}
	$q = "SELECT * FROM user_detail where user_name='$user' and user_passwd='$pass';";
	$r = getPG($q);
	if(!$r[0]) return false;
	else{
		$_SESSION['edoc_user'] = $user;
		$_SESSION['edoc_pass'] = $pass;
		$_SESSION['edoc_user_id'] = $r[0]['user_id'];
		$_SESSION['edoc_read'] = $r[0]['read']; //0=none 1=only own 2=all
		$_SESSION['edoc_write'] = $r[0]['write']; //0=none 1=only own 2=all
		$_SESSION['edoc_type'] = $r[0]['user_type']; //0=admin 1=normal
		$_SESSION['edoc_depart_id'] = $r[0]['depart_id'];
		$_SESSION['edoc_depart'] = $r[0]['depart_name'];
		$_SESSION['edoc_secret'] = $r[0]['secret'];
	}
	return true;
}

function moo_crypt($input){
	$key1 = '$1$edocdead$';
	$key2 = '$1$sesesese$';
	$key3 = '$1$xoneedoc$';

	$encrypted_data = crypt($input, $key1);
	$encrypted_data = explode($key1,$encrypted_data);

	$encrypted_data = crypt($encrypted_data[1], $key2);
	$encrypted_data = explode($key2,$encrypted_data);

	$encrypted_data = crypt($encrypted_data[1], $key3);
	$encrypted_data = explode($key3,$encrypted_data);

	return $encrypted_data[1];
}

function addlog($oper,$detail){
	$q = "insert into log (user_id,user_name,depart_id,depart_name,operation,detail) values (".$_SESSION['edoc_user_id'].",'".$_SESSION['edoc_user']."',".$_SESSION['edoc_depart_id'].",'".$_SESSION['edoc_depart']."','$oper','$detail');";
	updatePG($q);
}

function list_user(){
	echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" >\n";
	echo "<tr><td>ชื่อผู้ใช้</td><td>หน่วยงาน</td><td>การเรียกดู</td><td>การแก้ไข</td><td>ประเภทผู้ใช้</td><td>ชั้นความลับ</td><td>ลบผู้ใช้</td></tr>\n";
	$q = "select * from user_detail order by depart_name;";
	$ans = getPG($q);
	foreach($ans as $dat){
		if($dat['read']==0) $read = "ดูหนังสือไม่ได้";
		else if($dat['read']==1) $read = "เฉพาะหน่วยงาน";
		else if($dat['read']==2) $read = "ทุกหน่วยงาน";
		if($dat['write']==0) $write = "แก้หนังสือไม่ได้";
		else if($dat['write']==1) $write = "เฉพาะหน่วยงาน";
		else if($dat['write']==2) $write = "ทุกหน่วยงาน";
		if($dat['user_type']==0) $type = "ผู้ดูแลระบบ";
		else if($dat['user_type']==1) $type = "ทั่วไป";
		echo "<tr><td><a href=\"auser.php?comm=edit&uid=".$dat['user_id']."\">".$dat['user_name']."</a></td><td>".$dat['depart_name']."</td><td>".$read."</td><td>".$write."</td><td>".$type."</td><td>".shSecret($dat['secret'])."</td><td><a href=\"auser.php?comm=del&uid=".$dat['user_id']."&uname=".$dat['user_name']."\"><img src=\"pic/drop.png\" /></a></td></tr>\n";
	}	
	echo "</table>\n";
}

function list_user_secret($secret){
	echo "<optgroup label=\"secret\">\n";
	for($count=0;$count<4;$count++){
		if($count==$secret) echo "<input type=\"radio\" name=\"secret\" value=\"".$count."\" checked >".shSecret($count)." &nbsp;&nbsp;&nbsp;\n";
		else echo "<input type=\"radio\" name=\"secret\" value=\"".$count."\" >".shSecret($count)." &nbsp;&nbsp;&nbsp;\n";
	}
	echo "</optgroup>";
}

function list_user_rapid($rapid){
	echo "<optgroup label=\"rapid\">\n";
	for($count=0;$count<4;$count++){
		if($count==$rapid) echo "<input type=\"radio\" name=\"rapid\" value=\"".$count."\" checked >".shRapid($count)." &nbsp;&nbsp;&nbsp;\n";
		else echo "<input type=\"radio\" name=\"rapid\" value=\"".$count."\" >".shRapid($count)." &nbsp;&nbsp;&nbsp;\n";
	}
	echo "</optgroup>";
}

function shSecret($secret){
	if($secret==0) return "ปกติ";
	else if($secret==1) return "ลับ";
	else if($secret==2) return "ลับมาก";
	else if($secret==3) return "ลับที่สุด";
}

function shRapid($rapid){
	if($rapid==0) return "ปกติ";
	else if($rapid==1) return "ด่วน";
	else if($rapid==2) return "ด่วนมาก";
	else if($rapid==3) return "ด่วนที่สุด";
}


function list_depart_option(){
	$q =  "select * from department;";
	$ans = getPG($q);
	foreach($ans as $dat){
		if($dat['depart_id']==1)
			echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>";
		else
			echo "<option value=\"".$dat['depart_id']."\" >".$dat['depart_name']."</option>";
	}
}

function list_depart_option_selected($depart_id){
	$q =  "select * from department;";
	$ans = getPG($q);
	foreach($ans as $dat){
		if($dat['depart_id']==$depart_id) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>";
		else echo "<option value=\"".$dat['depart_id']."\" >".$dat['depart_name']."</option>";
	}
}

function chpasswd($opass,$npass1,$npass2){
	if($_SESSION['edoc_pass']!=moo_crypt($opass)){
		addlog("เปลี่ยนรหัสผ่าน","ไม่สามารถเปลี่ยนรหัสผ่านได้ เนื่องจากรหัสผ่านเดิมไม่ถูกต้อง");
		return -1;
	}
	if($npass1!=$npass2){
		addlog("เปลี่ยนรหัสผ่าน","ไม่สามารถเปลี่ยนรหัสผ่านได้ เนื่องจากรหัสตรวจสอบไม่ตรงกัน");
		return -2;
	}
	
	$username = $_SESSION['edoc_user'];
	$npass = moo_crypt($npass1);
	$q = "update edoc_user set user_passwd='$npass' where user_name='$username'";

	updatePG($q);

	$_SESSION['edoc_pass']=$npass;
	addlog("เปลี่ยนรหัสผ่าน","เปลี่ยนรหัสผ่านเรียบร้อยแล้ว");

	return 0;
}


function adduser($uname,$upasswd,$udepart,$udepart_add,$uread,$uwrite,$utype,$secret){
	$depart = $udepart;
	$udepart_add_new = trim($udepart_add);
	if($udepart==0){
		if($udepart_add_new==""){
			addlog("เพิ่มผู้ใช้","ไม่สามารถเพิ่มหน่วยงาน $udepart_add_new ได้");
			return -1;
		}
		$q = "insert into department (depart_name) values ('$udepart_add_new');";
		$r = updatePG($q);
		if($r==FALSE){
			addlog("เพิ่มผู้ใช้","ไม่สามารถเพิ่มหน่วยงาน $udepart_add_new ได้");
			return -1; //add department error
		}
		addlog("เพิ่มผู้ใช้","เพิ่มหน่วยงาน $udepart_add_new เรียบร้อยแล้ว");
		$q = "select * from department where depart_name='$udepart_add';";
		$r = getPG($q);
		$depart = $r[0]['depart_id'];
	}
	
	$upasswdd = moo_crypt($upasswd);
	$q = "insert into edoc_user (user_name,user_passwd,read,write,user_type,depart_id,secret) values ('$uname','$upasswdd',$uread,$uwrite,$utype,$depart,$secret);";
	$r = updatePG($q);
	if($r==FALSE){
		addlog("เพิ่มผู้ใช้","ไม่สามารถเพิ่มผู้ใช้ $uname เพราะชื่อผู้ใช้ซ้ำ");
		return -2; //add user error
	}
	addlog("เพิ่มผู้ใช้","เพิ่มผู้ใช้ $uname เรียบร้อยแล้ว");
	return 0;
}

function edituser($uid,$udepart,$udepart_add,$uread,$uwrite,$utype,$uname,$secret){
	$depart = $udepart;
	$udepart_add_new = trim($udepart_add);
	if($udepart==0){
		if($udepart_add_new=="")
		{
			addlog("แก้ไขผู้ใช้","ไม่สามารถเพิ่มหน่วยงาน $udepart_add_new ได้");
			return -1;
		}
		$q = "insert into department (depart_name) values ('$udepart_add_new');";
		$r = updatePG($q);
		if($r==FALSE){
			addlog("แก้ไขผู้ใช้","ไม่สามารถเพิ่มหน่วยงาน $udepart_add_new ได้");
			return -1; //add department error
		}
		addlog("แก้ไขผู้ใช้","เพิ่มหน่วยงาน $udepart_add_new เรียบร้อยแล้ว");
		$q = "select * from department where depart_name='$udepart_add';";
		$r = getPG($q);
		$depart = $r[0]['depart_id'];
	}
	
	$q = "update edoc_user set read=$uread,write=$uwrite,user_type=$utype,depart_id=$depart,secret=$secret where user_id=$uid;";
	$r = updatePG($q);
	if($r==FALSE){
		addlog("แก้ไขผู้ใช้","ไม่สามารถแก้ไขผู้ใช้ $uname ได้");
		return -2; //add user error
	}
	addlog("แก้ไขผู้ใช้","แก้ไขผู้ใช้ $uname เรียบร้อยแล้ว");	
	return 0;
}

function deluser($uid,$uname){
	$q = "delete from edoc_user where user_id=$uid;";
	$r = updatePG($q);
	if($r==FALSE){
		addlog("ลบผู้ใช้","ไม่สามารถลบผู้ใช้ $uname ได้");
		return -1; //del user error
	}
	addlog("ลบผู้ใช้","ลบผู้ใช้ $uname เรียบร้อยแล้ว");
	return 0;
}

function get_user_info($uid){
	$q = "select * from user_detail where user_id=$uid;";
	$r = getPG($q);
	return $r[0];
}

function list_department(){
	$q = "select * from department order by depart_name;";
	$r = getPG($q);
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"1\"><tr><td>หน่วยงาน</td><td>ลบหน่วยงาน</td></tr>\n";
	foreach($r as $dat){
		echo "<tr><td><a href=\"update.php?domain=depart&comm=edit&depart_id=".$dat['depart_id']."&depart_name=".$dat['depart_name']."\">".$dat['depart_name']."</a></td><td><a href=\"update.php?domain=depart&comm=del&depart_id=".$dat['depart_id']."&depart_name=".$dat['depart_name']."\"><img src=\"pic/drop.png\" /></a></td></tr>\n";
	}
	echo "</table>";
}

function delDepart($depart_id,$depart_name){
	$q = "delete from department where depart_id=$depart_id;";
	$r = updatePG($q);
	if($r==FALSE){
		addlog("ลบหน่วยงาน","ไม่สามารถลบหน่วยงาน $depart_name ได้");
		return -1; //delete department error
	}
	addlog("ลบหน่วยงาน","ลบหน่วยงาน $depart_name เรียบร้อยแล้ว");
	return 0;
}

function add_depart($depart_name){
	$q = "insert into department (depart_name) values ('$depart_name');";
	$r = updatePG($q);
	if($r==FALSE)
	{
		addlog("เพิ่มหน่วยงาน","ไม่สามารถเพิ่มหน่วยงาน $depart_name ได้");
		return -1; //add department error
	}
	addlog("เพิ่มหน่วยงาน","เพิ่มหน่วยงาน $depart_name เรียบร้อยแล้ว");
	return 0;
}

function update_depart($depart_id,$depart_name,$depart_name_old){
	$q = "update department set depart_name='$depart_name' where depart_id=$depart_id;";
	$r = updatePG($q);
	if($r==FALSE){
		addlog("แก้ไขหน่วยงาน","ไม่สามารถแก้ไขหน่วยงาน $depart_name_old ไปเป็น $depart_name ได้");
		return -1; //update department error
	}
	addlog("แก้ไขหน่วยงาน","แก้ไขหน่วยงาน $depart_name_old ไปเป็น $depart_name เรียบร้อยแล้ว");
	return 0;
}

function list_department_user($depart,$form){
	echo "<select name=\"depart_id\" size=\"1\" onchange=\"document.getElementById('".$form."').submit()\">\n";

	if($_SESSION['edoc_write']==1){
		$q = "select * from department where depart_id=".$_SESSION['edoc_depart_id'].";";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart==0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
			else echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart!=0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$depart) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
			else echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";	
		}
	}

	echo "</select>\n";
}

function list_department_userR($depart,$form){
	echo "<select name=\"depart_id\" size=\"1\" onchange=\"document.getElementById('".$form."').submit()\">\n";

	if($_SESSION['edoc_read']==1){
		$q = "select * from department where depart_id=".$_SESSION['edoc_depart_id'].";";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart==0 & $_SESSION['edoc_read']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
			else echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart!=0 & $_SESSION['edoc_read']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$depart) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
			else echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";	
		}
	}

	echo "</select>\n";
}

function list_department_userS($depart){
	echo "<select name=\"depart_id\" size=\"1\" onchange=\"doctypech()\" id=\"xdepart\">\n";
	
	if($_SESSION['edoc_read']==1){
		$q = "select * from department where depart_id=".$_SESSION['edoc_depart_id'].";";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart==0 & $_SESSION['edoc_read']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		echo "<option value=\"0\" selected>ทั้งหมด</option>\n";
		foreach($ans as $dat){
			echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";
		}
	}
	else if($depart!=0 & $_SESSION['edoc_read']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		echo "<option value=\"0\">ทั้งหมด</option>\n";
		foreach($ans as $dat){
			if($dat['depart_id']==$depart) echo "<option value=\"".$dat['depart_id']."\" selected>".$dat['depart_name']."</option>\n";
			else echo "<option value=\"".$dat['depart_id']."\">".$dat['depart_name']."</option>\n";	
		}
	}

	echo "</select>\n";
}

function list_department_userX($depart){

	if($_SESSION['edoc_write']==1){
		$q = "select * from department where depart_id=".$_SESSION['edoc_depart_id'].";";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			 }
		}
	}
	else if($depart==0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			}
		}
	}
	else if($depart!=0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$depart){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			}
		}
	}

	echo "</select>\n";
}

function list_doctype($depart){
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"1\"><tr><td>ประเภทหนังสือ</td><td>ลบประเภทหนังสือ</td></tr>\n";
	
	if($_SESSION['edoc_write']==1) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart==0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart!=0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$depart.";"; 

	$ans = getPG($q);
	
	foreach($ans as $dat){
		echo "<tr><td><a href=\"update.php?domain=doctype&comm=edit&doctype_id=".$dat['edoc_type_id']."&doctype_name=".$dat['edoc_type_name']."&depart=".$dat['depart_name']."\">".$dat['edoc_type_name']."</a></td><td><a href=\"update.php?domain=doctype&comm=del&doctype_id=".$dat['edoc_type_id']."&doctype_name=".$dat['edoc_type_name']."&depart=".$dat['depart_name']."\"><img src=\"pic/drop.png\" /></a></td></tr>\n";
	}
	
	echo "</table>\n";
}

function add_doctype($doctype_name,$depart_id,$depart_name){
	$q = "insert into edoc_type (edoc_type_name,depart_id) values ('$doctype_name',$depart_id);";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("เพิ่มประเภทหนังสือ","ไม่สามารถเพิ่มประเภทหนังสือ ".$doctype_name." ในหน่วยงาน ".$depart_name." ได้");
		return -1; //error to add doctype
	}
	addlog("เพิ่มประเภทหนังสือ","เพิ่มประเภทหนังสือ ".$doctype_name." ในหน่วยงาน ".$depart_name." เรียบร้อยแล้ว");
	return 0;
}

function get_depart_name($depart_id){
	if($depart_id==0) return $_SESSION['edoc_depart'];
	$q = "select depart_name from department where depart_id=$depart_id;";
	$ans = getPG($q);
	return $ans[0]['depart_name'];
}

function get_depart_name_with_run($depart_id){
	if($depart_id==0) return "สิ้นสุดการส่งต่อ";
	$q = "select depart_name from department where depart_id=$depart_id;";
	$ans = getPG($q);
	return $ans[0]['depart_name'];
}

function get_depart_id($depart_name){
	$q = "select depart_id from department where depart_name='$depart_name';";
	$ans = getPG($q);
	return $ans[0]['depart_id'];
}

function get_position_name($position_id){
	$q = "select position_name from position where position_id=$position_id;";
	$ans = getPG($q);
	return $ans[0]['position_name'];
}

function del_doctype($doctype_id,$doctype_name,$depart){
	$q = "delete from edoc_type where edoc_type_id=$doctype_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("ลบประเภทหนังสือ","ไม่สามารถลบประเภทหนังสือ ".$doctype_name." ในหน่วยงาน ".$depart." ได้");
		return -1; //del doctype error
	}
	addlog("ลบประเภทหนังสือ","ลบประเภทหนังสือ ".$doctype_name." ในหน่วยงาน ".$depart." เรียบร้อยแล้ว");
	return 0;
}

function edit_doctype($doctype_id,$doctype_name,$doctype_name_old,$depart){
	$q = "update edoc_type set edoc_type_name='$doctype_name' where edoc_type_id=$doctype_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("แก้ไขประเภทหนังสือ","ไม่สามารถแก้ไขประเภทหนังสือ ".$doctype_name_old." ไปเป็น ".$doctype_name." ในหน่วยงาน ".$depart." ได้");
		return -1; //edit doctype error
	}
	addlog("แก้ไขประเภทหนังสือ","แก้ไขประเภทหนังสือ ".$doctype_name_old." ไปเป็น ".$doctype_name." ในหน่วยงาน ".$depart." เรียบร้อยแล้ว");
	return 0;
}

function list_position($depart){
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"1\"><tr><td>ตำแหน่ง</td><td>ลบตำแหน่ง</td></tr>\n";
	
	if($_SESSION['edoc_write']==1) $q = "select * from position_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart==0 & $_SESSION['edoc_write']==2) $q = "select * from position_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart!=0 & $_SESSION['edoc_write']==2) $q = "select * from position_detail where depart_id=".$depart.";"; 

	$ans = getPG($q);
	
	foreach($ans as $dat){
		echo "<tr><td><a href=\"update.php?domain=position&comm=edit&position_id=".$dat['position_id']."&position_name=".$dat['position_name']."&depart=".$dat['depart_name']."\">".$dat['position_name']."</a></td><td><a href=\"update.php?domain=position&comm=del&position_id=".$dat['position_id']."&position_name=".$dat['position_name']."&depart=".$dat['depart_name']."\"><img src=\"pic/drop.png\" /></a></td></tr>\n";
	}
	
	echo "</table>\n";
}

function add_position($position_name,$depart_id,$depart_name){
	$q = "insert into position (position_name,depart_id) values ('$position_name',$depart_id);";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("เพิ่มตำแหน่ง","ไม่สามารถเพิ่มตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart_name." ได้");
		return -1; //error to add position
	}
	addlog("เพิ่มตำแหน่ง","เพิ่มตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart_name." เรียบร้อยแล้ว");
	return 0;
}

function del_position($position_id,$position_name,$depart){
	$q = "delete from position where position_id=$position_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("ลบตำแหน่ง","ไม่สามารถลบตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart." ได้");
		return -1; //del position error
	}
	addlog("ลบตำแหน่ง","ลบตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart." เรียบร้อยแล้ว");
	return 0;
}

function edit_position($position_id,$position_name,$position_name_old,$depart){
	$q = "update position set position_name='$position_name' where position_id=$position_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("แก้ไขตำแหน่ง","ไม่สามารถแก้ไขตำแหน่ง ".$position_name_old." ไปเป็น ".$position_name." ในหน่วยงาน ".$depart." ได้");
		return -1; //edit position error
	}
	addlog("แก้ไขตำแหน่ง","แก้ไขตำแหน่ง ".$position_name_old." ไปเป็น ".$position_name." ในหน่วยงาน ".$depart." เรียบร้อยแล้ว");
	return 0;
}

function list_pname($depart){
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"1\"><tr><td>ผู้ดำรงตำแหน่ง</td><td>ตำแหน่ง</td><td>การดำรงตำแหน่ง</td><td>ลบตำแหน่ง</td></tr>\n";
	
	if($_SESSION['edoc_write']==1) $q = "select * from pname_detail where depart_id=".$_SESSION['edoc_depart_id']." order by pname_status desc;"; 
	else if($depart==0 & $_SESSION['edoc_write']==2) $q = "select * from pname_detail where depart_id=".$_SESSION['edoc_depart_id']." order by pname_status desc;"; 
	else if($depart!=0 & $_SESSION['edoc_write']==2) $q = "select * from pname_detail where depart_id=".$depart." order by pname_status desc;"; 

	$ans = getPG($q);
	
	foreach($ans as $dat){
		echo "<tr><td><a href=\"update.php?domain=pname&comm=edit&pname_id=".$dat['pname_id']."&pname_name=".$dat['pname_name']."&position_name=".$dat['position_name']."&depart=".$dat['depart_name']."\">".$dat['pname_name']."</a></td><td>".$dat['position_name']."</td><td><a href=\"update.php?domain=pname&comm=chstat&pname_id=".$dat['pname_id']."&pname_name=".$dat['pname_name']."&position_name=".$dat['position_name']."&depart=".$dat['depart_name']."\">".showPstat($dat['pname_status'])."</a></td><td><a href=\"update.php?domain=pname&comm=del&pname_id=".$dat['pname_id']."&pname_name=".$dat['pname_name']."&position_name=".$dat['position_name']."&depart=".$dat['depart_name']."\"><img src=\"pic/drop.png\" /></a></td></tr>\n";
	}
	
	echo "</table>\n";

}

function showPstat($stat){
	if($stat=='t') return "ดำรงตำแหน่ง";
	else return "ออกจากตำแหน่ง";
}

function list_position_department($depart){
	if($depart==0) $depart_id = $_SESSION['edoc_depart_id'];
	else $depart_id = $depart;
	
	echo "<select name=\"position\" size=\"1\">\n";
	
	$q = "select * from position_detail where depart_id=$depart_id;";
	$ans = getPG($q);
	
	echo "<option value=\"0\">เพิ่มตำแหน่ง</option>\n";
	
	foreach($ans as $dat){
		echo "<option value=\"".$dat['position_id']."\">".$dat['position_name']."</option>\n";
	}
	
	echo "</select>";
}

function list_position_select($depart){
	echo "<select name=\"position\" size=\"1\" id=\"xposition\" onchange=\"pnamech()\">\n";

	$q = "select * from position_detail where depart_id=$depart;";
	$ans = getPG($q);
	
	foreach($ans as $dat){
		echo "<option value=\"".$dat['position_id']."\">".$dat['position_name']."</option>\n";
	}
	
	echo "</select>";
}

function add_pname($pname_name,$depart_id,$depart_name,$position_id,$position_name){
	if($position_id==0){
		$position_n = trim($position_name);
		if($position_n==""){
			addlog("เพิ่มผู้ดำรงตำแหน่ง","ไม่สามารถเพิ่มตำแหน่ง ".$position_n." ในหน่วยงาน ".$depart_name." ได้");
			return -2;
		}

		$q = "insert into position (position_name,depart_id) values ('$position_n',$depart_id);";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("เพิ่มผู้ดำรงตำแหน่ง","ไม่สามารถเพิ่มตำแหน่ง ".$position_n." ในหน่วยงาน ".$depart_name." ได้");
			return -2;
		}

		$q = "select position_id from position where position_name='$position_n' and depart_id=$depart_id;";
		$ans = getPG($q);
		$position_idd = $ans[0]['position_id'];
	}
	else $position_idd = $position_id;

	$q = "insert into pname (pname_name,position_id) values ('$pname_name',$position_idd);";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("เพิ่มผู้ดำรงตำแหน่ง","ไม่สามารถเพิ่มผู้ดำรงตำแหน่ง ".$pname_name." ลงในตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart_name." ได้");
		return -1; //error to add pname
	}
	addlog("เพิ่มผู้ดำรงตำแหน่ง","เพิ่มผู้ดำรงตำแหน่ง ".$pname_name." ลงในตำแหน่ง ".$position_name." ในหน่วยงาน ".$depart_name." เรียบร้อยแล้ว");
	return 0;
}

function del_pname($pname_id,$pname_name,$position_name,$depart){
	$q = "delete from pname where pname_id=$pname_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("ลบผู้ดำรงตำแหน่ง","ไม่สามารถลบผู้ดำรงตำแหน่ง $pname_name ดำรงตำแหน่ง $position_name ในหน่วยงาน $depart ได้");
		return -1; //del pname error
	}
	addlog("ลบผู้ดำรงตำแหน่ง","ลบผู้ดำรงตำแหน่ง $pname_name ดำรงตำแหน่ง $position_name ในหน่วยงาน $depart เรียบร้อยแล้ว");
	return 0;
}

function edit_pname($pname_id,$pname_name,$pname_name_old,$position_name,$depart){
	$q = "update pname set pname_name='$pname_name' where pname_id=$pname_id;";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("แก้ไขผู้ดำรงตำแหน่งตำแหน่ง","ไม่สามารถแก้ไขผู้ดำรงตำแหน่ง ".$pname_name_old." ไปเป็น ".$pname_name." ดำรงตำแหน่ง $position_name ในหน่วยงาน ".$depart." ได้");
		return -1; //edit pname error
	}
	addlog("แก้ไขผู้ดำรงตำแหน่ง","แก้ไขผู้ดำรงตำแหน่ง ".$pname_name_old." ไปเป็น ".$pname_name." ดำรงตำแหน่ง $position_name ในหน่วยงาน ".$depart." เรียบร้อยแล้ว");
	return 0;
}

function chstat_pname($pname_id,$pname_name,$position_name,$depart){
	$q = "select pname_status from pname where pname_id=$pname_id;";
	$ans = getPG($q);
	
	if($ans[0]['pname_status']=='t'){
		$q = "update pname set pname_status='f' where pname_id=$pname_id;";
		addlog("เปลี่ยนสถานะผู้ดำรงตำแหน่ง","เปลี่ยนสถานะผู้ดำรงตำแหน่ง $pname_name ออกจากตำแหน่ง $position_name ในหน่วยงาน $depart เรียบร้อยแล้ว");
	}
	else{
		$q = "update pname set pname_status='t' where pname_id=$pname_id;";
		addlog("เปลี่ยนสถานะผู้ดำรงตำแหน่ง","เปลี่ยนสถานะผู้ดำรงตำแหน่ง $pname_name กลับมาดำรงตำแหน่ง $position_name ในหน่วยงาน $depart เรียบร้อยแล้ว");
	}

	$ans = updatePG($q);
}

function get_stat_pname($pname_id){
	$q = "select pname_status from pname where pname_id=$pname_id;";
	$ans = getPG($q);
	return $ans[0]['pname_status'];
}

function list_doctype_option($depart){
	echo "<select name=\"doctype\" size=\"1\">\n";
	
	if($_SESSION['edoc_write']==1) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart==0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart!=0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$depart.";"; 

	$ans = getPG($q);
	echo "<option value=\"0\" selected>เพิ่มประเภทหนังสือ</option>\n";
	
	foreach($ans as $dat){
		echo "<option value=\"".$dat['edoc_type_id']."\">".$dat['edoc_type_name']."</option>\n";
	}
	
	echo "</select>\n";
}

function list_doctype_option_no_add($depart){
	echo "<select name=\"doctype\" size=\"1\">\n";
	
	if($depart==0){
		echo "<option value=\"0\">---</option>\n";
		echo "</select>\n";
		return;
	}
	
	if($_SESSION['edoc_read']==1) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart==0 & $_SESSION['edoc_read']==2) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart!=0 & $_SESSION['edoc_read']==2) $q = "select * from edoc_type_detail where depart_id=".$depart.";"; 

	$ans = getPG($q);
	
	echo "<option value=\"0\">ทั้งหมด</option>\n";
		
	foreach($ans as $dat){
		echo "<option value=\"".$dat['edoc_type_id']."\">".$dat['edoc_type_name']."</option>\n";
	}
	
	echo "</select>\n";
}


function list_doctype_option_default($depart,$doc_type){
	echo "<select name=\"doctype\" size=\"1\">\n";
	
	if($_SESSION['edoc_write']==1) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart==0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$_SESSION['edoc_depart_id'].";"; 
	else if($depart!=0 & $_SESSION['edoc_write']==2) $q = "select * from edoc_type_detail where depart_id=".$depart.";"; 

	$ans = getPG($q);
	echo "<option value=\"0\" >เพิ่มประเภทหนังสือ</option>\n";
	
	foreach($ans as $dat){
		if($dat['edoc_type_id']==$doc_type)		
			echo "<option value=\"".$dat['edoc_type_id']."\" selected >".$dat['edoc_type_name']."</option>\n";
		else
			echo "<option value=\"".$dat['edoc_type_id']."\" >".$dat['edoc_type_name']."</option>\n";
	}
	
	echo "</select>\n";
}


function addDoc($recieve_no,$recieve_date,$recieve_time,$doc_no,$doc_date,$secret,$rapid,$doctype,$doctype_add,$depart_send_id,$depart_send_name_out,$reciever,$headline,$sender,$comment,$pdf,$depart_idd){
	if($depart_idd==0) $depart_iddd = $_SESSION['edoc_depart_id'];	
	else $depart_iddd = $depart_idd;
	$doctype_n = $edoctype_add;
	$depart_send_name_out1 = trim($depart_send_name_out);
	$doc_no1 = str_replace(" ","",$doc_no);
	$recieve_time1 = str_replace(".",":",$recieve_time);
	$recieve_no1 = str_replace(" ","",$recieve_no);
	if($doctype==0){
		$doctype_n = trim($doctype_add);
		if($doctype_n==""){
			addlog("เพิ่มหนังสือ","ไม่สามารถเพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." ได้");
			return -1;
		}

		$q = "insert into edoc_type (edoc_type_name,depart_id) values ('$doctype_n',$depart_iddd);";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("เพิ่มหนังสือ","ไม่สามารถเพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." ได้");
			return -1;
		}
		addlog("เพิ่มหนังสือ","เพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." เรียบร้อยแล้ว");
		$q = "select edoc_type_id from edoc_type where edoc_type_name='$doctype_n' and depart_id=$depart_iddd;";
		$ans = getPG($q);
		$doctype1 = $ans[0]['edoc_type_id'];
	}
	else $doctype1 = $doctype;
	
	if($depart_send_id==0){
		$depart_send_n = trim($depart_send_name_out);
		$depart_send_name_out1 = "";
		if($depart_send_n==""){
			addlog("เพิ่มหนังสือ","ไม่สามารถเพิ่มหน่วยงาน $depart_send_n ได้");
			return -2;
		}

		$q = "insert into department (depart_name) values ('$depart_send_n');";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("เพิ่มหนังสือ","ไม่สามารถเพิ่มหน่วยงาน $depart_send_n ได้");
			return -2;
		}
		addlog("เพิ่มหนังสือ","เพิ่มหน่วยงาน $depart_send_n เรียบร้อยแล้ว");
		$q = "select depart_id from department where depart_name='$depart_send_n';";
		$ans = getPG($q);
		$depart_send_id1 = $ans[0]['depart_id'];
	}
	else $depart_send_id1 = $depart_send_id;
	
	$recieve_datex = dateThEnco($recieve_date);
	$doc_datex = dateThEnco($doc_date);

	if($pdf['size']>0){
		$pdf_c = file_get_contents($pdf['tmp_name']);
		$pdf_c = pg_escape_bytea($pdf_c);
		$pdf_name1 = $pdf['name'];
		$pdf_type1 = $pdf['type'];
		
	}else{
		$pdf_c = "";
		$pdf_c = pg_escape_bytea($pdf_c);
		$pdf_name1 = "";
		$pdf_type1 = "";

	}

	$q = "insert into edoc (recieve_no,recieve_date,recieve_time,doc_no,doc_date,sender,reciever,headline,comment,edoc_type_id,depart_send_id,depart_send_name_out,secret,rapid,pdf,pdf_name,pdf_type,depart_run_id) values ('$recieve_no1','$recieve_datex','$recieve_time1','$doc_no1','$doc_datex','$sender','$reciever','$headline','$comment',$doctype1,$depart_send_id1,'$depart_send_name_out1',$secret,$rapid,'{$pdf_c}','$pdf_name1','$pdf_type1',$depart_iddd);";
	
	$ans = updatePG($q);

	if($ans==FALSE){
		addlog("เพิ่มหนังสือ","ไม่สามารถเพิ่มหนังสือได้");
		return -3;
	}
	
	addlog("เพิ่มหนังสือ","เพิ่มหนังสือเลขที่ $doc_no เรียบร้อยแล้ว");
	return 0;
}
	
function editDoc($edoc_id,$recieve_no,$recieve_date,$recieve_time,$doc_no,$doc_date,$secret,$rapid,$doctype,$doctype_add,$depart_send_id,$depart_send_name_out,$reciever,$headline,$sender,$comment,$pdf,$depart_run_id,$depart_idd){
	$dat = getDocInfo($edoc_id);
	$right = getRightW($dat['edoc_id']);
	if($right==FALSE){
		addlog("แก้ไขหนังสือ","ไม่สามารถแก้ไขหนังสือได้");
		return -3;
	}	

	if($depart_idd==0) $depart_iddd = $_SESSION['edoc_depart_id'];	
	else $depart_iddd = $depart_idd;
	$doctype_n = $edoctype_add;
	$depart_send_name_out1 = trim($depart_send_name_out);
	$doc_no1 = str_replace(" ","",$doc_no);
	$recieve_time1 = str_replace(".",":",$recieve_time);
	$recieve_no1 = str_replace(" ","",$recieve_no);
	if($doctype==0){
		$doctype_n = trim($doctype_add);
		if($doctype_n==""){
			addlog("แก้ไขหนังสือ","ไม่สามารถเพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." ได้");
			return -1;
		}

		$q = "insert into edoc_type (edoc_type_name,depart_id) values ('$doctype_n',$depart_iddd);";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("แก้ไขหนังสือ","ไม่สามารถเพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." ได้");
			return -1;
		}
		addlog("แก้ไขหนังสือ","เพิ่มประเภทหนังสือ ".$doctype_n." ในหน่วยงาน ".get_depart_name($depart_iddd)." เรียบร้อยแล้ว");
		$q = "select edoc_type_id from edoc_type where edoc_type_name='$doctype_n' and depart_id=$depart_iddd;";
		$ans = getPG($q);
		$doctype1 = $ans[0]['edoc_type_id'];
	}
	else $doctype1 = $doctype;
	
	if($depart_send_id==0){
		$depart_send_n = trim($depart_send_name_out);
		$depart_send_name_out1 = "";
		if($depart_send_n==""){
			addlog("แก้ไขหนังสือ","ไม่สามารถเพิ่มหน่วยงาน $depart_send_n ได้");
			return -2;
		}

		$q = "insert into department (depart_name) values ('$depart_send_n');";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("แก้ไขหนังสือ","ไม่สามารถเพิ่มหน่วยงาน $depart_send_n ได้");
			return -2;
		}
		addlog("แก้ไขหนังสือ","เพิ่มหน่วยงาน $depart_send_n เรียบร้อยแล้ว");
		$q = "select depart_id from department where depart_name='$depart_send_n';";
		$ans = getPG($q);
		$depart_send_id1 = $ans[0]['depart_id'];
	}
	else $depart_send_id1 = $depart_send_id;
	
	$recieve_datex = dateThEnco($recieve_date);
	$doc_datex = dateThEnco($doc_date);

	if($pdf['size']>0){
		$pdf_c = file_get_contents($pdf['tmp_name']);
		$pdf_c = pg_escape_bytea($pdf_c);
		$pdf_name1 = $pdf['name'];
		$pdf_type1 = $pdf['type'];
		
	}else{
		$pdf_c = "";
		$pdf_c = pg_escape_bytea($pdf_c);
		$pdf_name1 = "";
		$pdf_type1 = "";

	}

	if($pdf['size']>0)	
		$q = "update edoc set recieve_no='$recieve_no1',recieve_date='$recieve_datex',recieve_time='$recieve_time1',doc_no='$doc_no1',doc_date='$doc_datex',sender='$sender',reciever='$reciever',headline='$headline',comment='$comment',edoc_type_id=$doctype1,depart_send_id=$depart_send_id1,depart_send_name_out='$depart_send_name_out1',secret=$secret,rapid=$rapid,pdf='{$pdf_c}',pdf_name='$pdf_name1',pdf_type='$pdf_type1',depart_run_id=$depart_run_id where edoc_id=$edoc_id;";
	else
		$q = "update edoc set recieve_no='$recieve_no1',recieve_date='$recieve_datex',recieve_time='$recieve_time1',doc_no='$doc_no1',doc_date='$doc_datex',sender='$sender',reciever='$reciever',headline='$headline',comment='$comment',edoc_type_id=$doctype1,depart_send_id=$depart_send_id1,depart_send_name_out='$depart_send_name_out1',secret=$secret,rapid=$rapid,depart_run_id=$depart_run_id where edoc_id=$edoc_id;";

	$ans = updatePG($q);

	if($ans==FALSE){
		addlog("แก้ไขหนังสือ","ไม่สามารถแก้ไขหนังสือได้");
		return -3;
	}
	
	addlog("แก้ไขหนังสือ","แก้ไขหนังสือเลขที่ $doc_no เรียบร้อยแล้ว");
	return 0;
}

function dateThEnco($d){
	$a = explode("/",$d);
	if(count($a)==3){
		$a[2] = $a[2]-543;
		$aa = $a[1]."/".$a[0]."/".$a[2];
		return $aa;
	}else{
		return $d;
	}
}

function dateThDeco($d){
	$a = explode("-",$d);
	if(count($a)==3){
		$a[0] = $a[0]+543;
		$aa = $a[2]."/".$a[1]."/".$a[0];
		return $aa;
	}else{
		return $d;
	}
}

function shEdocType($doctype,$doctype_add){
	if($doctype!=0){
		$q = "select edoc_type_name from edoc_type where edoc_type_id=$doctype;";
		$ans = getPG($q);
		return $ans[0]['edoc_type_name'];
	}else{
		return trim($doctype_add);
	}
}

function shDepartSend($depart_send_id,$depart_send_name_out){
	if($depart_send_id>1){
		$q = "select depart_name from department where depart_id=$depart_send_id;";
		$ans = getPG($q);
		return $ans[0]['depart_name'];
	}else if($depart_send_id==1) return "ภายนอก { ".trim($depart_send_name_out)." }";
	else return trim($depart_send_name_out);
}

function shDepartSendFast($depart_send_id,$dname_send,$depart_send_name_out){
	if($depart_send_id==1) return "ภายนอก { ".$depart_send_name_out." }";
	else return $dname_send;
}


function shPDFname($pdf){
	return $pdf['name'];
}

function get_edoc_id($doc_no,$recieve_no,$depart_id){

	$q = "select edoc_id from edoc_view_type where doc_no='$doc_no' and recieve_no='$recieve_no' and depart_id='$depart_id';";
	$ans = getPG($q);
	
	return $ans[0]['edoc_id'];
}

function shDocIn($depart_idd,$pagee){

	$secret_u = $_SESSION['edoc_secret'];
	if($depart_idd==$_SESSION['edoc_depart_id'])
		$q = "select * from edoc_view_data where depart_run_id=$depart_idd order by depart_id,recieve_date desc,recieve_no desc;";
	else
		$q = "select * from edoc_view_data where depart_run_id=$depart_idd and (secret<=$secret_u) order by depart_id,recieve_date desc,recieve_no desc;";
	$ans = getPG($q);
	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span>";
		return;
	}
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\"><tr><td>";

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id'];
	foreach($ans as $dat){
		if($dat['depart_id']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
			$depart = $dat['depart_id'];
			createDocList($dat2,$pagee);
			echo "</td></tr><tr><td>";
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
	createDocList($dat2,$pagee);
	echo "</td></tr></table>";
}

function shDocOut($depart_idd,$pagee){
	
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_idd==$_SESSION['edoc_depart_id'])	
		$q = "select * from edoc_view_data where depart_id=$depart_idd and not(depart_run_id=0) and not(depart_run_id=depart_id) order by depart_run_id,recieve_date desc,recieve_no desc;";
	else
		$q = "select * from edoc_view_data where depart_id=$depart_idd and not(depart_run_id=0) and not(depart_run_id=depart_id) and (secret<=$secret_u) order by depart_run_id,recieve_date desc,recieve_no desc;";
	$ans = getPG($q);
	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span>";
		return;
	}
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\"><tr><td>";

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_run_id'];
	foreach($ans as $dat){
		if($dat['depart_run_id']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
			$depart = $dat['depart_run_id'];
			createDocList($dat2,$pagee);
			echo "</td></tr><tr><td>";
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
	createDocList($dat2,$pagee);
	echo "</td></tr></table>";
}

function shDocSearch($condition,$pagee){
	$q = "select * from edoc_view_data ".$condition." order by depart_id,recieve_date desc,recieve_no desc;";
	$ans = getPG($q);
	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span><br />";
		return;
	}else if($ans==FALSE){
		echo "<span class=\"err\">การค้นหาผิดพลาด</span><br />";
		return;
	}
	echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\"><tr><td>";

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id'];
	foreach($ans as $dat){
		if($dat['depart_id']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
			$depart = $dat['depart_id'];
			createDocList($dat2,$pagee);
			echo "</td></tr><tr><td>";
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	echo "<span class=\"topic\">".get_depart_name($depart)."</span><br />";
	createDocList($dat2,$pagee);
	echo "</td></tr></table>";

}

function pdfCHK($dat){
	if($dat=="") return "";
	return "<img src=\"pic/pdfsmall.jpg\" />";
}

function getRightW($edoc_id){

	$doc = getDocInfo($edoc_id);

	if(($_SESSION['edoc_write']==1) && ($doc['depart_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_write']==2) && ($_SESSION['edoc_secret']>=$doc['secret'])) return TRUE;
	return FALSE;

}

function getRightK($edoc_id){

	if(getRightR($edoc_id)==TRUE) return TRUE;	
	
	$doc = getDocInfo($edoc_id);

	if(($_SESSION['edoc_write']==1) && ($doc['depart_run_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_write']==2) && ($_SESSION['edoc_secret']>=$doc['secret'])) return TRUE;
	return FALSE;

}

function getRightKK($edoc_id){

	$doc = getDocInfo($edoc_id);

	if(($_SESSION['edoc_write']==1) && ($doc['depart_run_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_write']==2) && ($_SESSION['edoc_secret']>=$doc['secret'])) return TRUE;
	return FALSE;

}

function getRightR($edoc_id){

	$doc = getDocInfo($edoc_id);
	
	if(($_SESSION['edoc_read']==1) && ($doc['depart_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_write']==1) && ($doc['depart_run_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_read']==1) && ($doc['depart_run_id']==$_SESSION['edoc_depart_id'])) return TRUE;
	else if(($_SESSION['edoc_read']==2) && ($_SESSION['edoc_secret']>=$doc['secret'])) return TRUE;
	return FALSE;

}

function createDocList($l,$pagee){
?>
<table cellpadding="2" cellspacing="0" border="1" valign="top" width="100%">
<tr>
<td>เลขที่รับ</td>
<td>เลขที่หนังสือ</td>
<td>วันที่หนังสือ</td>
<td>ชั้นความลับ</td>
<td>ความเร่งด่วน</td>
<td>จากหน่วยงาน</td>
<td>เรื่อง</td>
<td>ผู้ส่ง</td>
<td>PDF</td>
<td>เกษียณ</td>
<td>แก้ไข</td>
<td>หมายเหตุ</td>
<td>ลบ</td>
</tr>
<?php
	foreach($l as $dat){
		$rightW = getRightW($dat['edoc_id']);
		echo "<tr><td>".$dat['recieve_no']."</td>";
		echo "<td>".$dat['doc_no']."</td><td>".dateThDeco($dat['doc_date'])."</td>";
		echo "<td>".shSecret($dat['secret'])."</td><td>".shRapid($dat['rapid'])."</td>";
		echo "<td>".shDepartSendFast($dat['depart_send_id'],$dat['dname_send'],$dat['depart_send_name_out'])."</td>";
		echo "<td>".$dat['headline']."</td><td>".nl2br(htmlspecialchars($dat['sender']))."</td>";
		echo "<td><a href=\"loadpdf.php?edoc_id=".$dat['edoc_id']."\" >".pdfCHK($dat['pdf_type'])."</a></td>";
		if(getRightK($dat['edoc_id'])==TRUE) echo "<td><a href=\"ksean.php?edoc_id=".$dat['edoc_id']."\" ><img src=\"pic/ksean.jpg\" /></a></td>";
		else echo "<td> </td>";
		if($rightW==TRUE) echo "<td><a href=\"edit_doc.php?comm=edit&edoc_id=".$dat['edoc_id']."\" target=\"_blank\" ><img src=\"pic/edit.png\" /></a></td>";
		else echo "<td></td>";
		echo "<td>".nl2br(htmlspecialchars($dat['comment']))."</td>";
		if($rightW==TRUE) echo "<td><a href=\"".$pagee."comm=del&edoc_id=".$dat['edoc_id']."&doc_no=".$dat['doc_no']."&depart_name=".$dat['depart_name']."\" ><img src=\"pic/drop.png\" /></a></td>";
		else echo "<td></td>";
		echo "</tr>";
	}
	echo "</table>";
}

function getDocInfo($edoc_id){
	$q = "select * from edoc_view_data where edoc_id=$edoc_id;";
	$ans = getPG($q);
	if(isset($ans[0])) return $ans[0];
	return FALSE;
}

function delDoc($edoc_id){
	$dat = getDocInfo($edoc_id);
	$right = getRightW($dat['edoc_id']);
	if($right==TRUE){
		$q = "delete from edoc where edoc_id=$edoc_id;";
		$ans = updatePG($q);
		if($ans==FALSE){
			addlog("ลบหนังสือ","ไม่สามารถลบหนังสือเลขที่ ".$dat['doc_no']." ในหน่วยงาน ".$dat['depart_name']." ได้");
			return -1;
		}
		addlog("ลบหนังสือ","ลบหนังสือเลขที่ ".$dat['doc_no']." ในหน่วยงาน ".$dat['depart_name']." เรียบร้อยแล้ว");
		return 0;
	}else return -1;
}

function save_ksean($edoc_id,$ksean_txt,$pname,$ksean_date,$doc_no){
	$ksean_datex = dateThEnco($ksean_date);
	$q = "insert into ksean (edoc_id,pname_id,ksean_txt,ksean_date) values ($edoc_id,$pname,'$ksean_txt','$ksean_datex')";
	$ans = updatePG($q);
	if($ans==FALSE){
		$dat = getDocInfo($edoc_id);
		addlog("เกษียณหนังสือ","ไม่สามารถเกษียณหนังสือเลขที่ ".$doc_no." ได้");
		return -1;
	}
	addlog("เกษียณหนังสือ","เกษียณหนังสือเลขที่ ".$doc_no." เรียบร้อยแล้ว");
	return 0;
}

function listKsean($edoc_id){
	$q = "select * from ksean_view where edoc_id=$edoc_id order by ksean_id,ksean_date;";
	$ans = getPG($q);
	
	if(!isset($ans[0])){
		echo "<br /><span class=\"err\" >ไม่มีรายการเกษียณ</span><br /><br />";
		return;
	}	
	
	$doc = getDocInfo($edoc_id); 
	
	echo "<br />\n";
	foreach($ans as $dat){
		echo "<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\">";
		echo "<tr><td>ข้อความเกษียณ</td><td>:</td><td>".nl2br($dat['ksean_txt'])."</td></tr>";
		echo "<tr><td>ผู้ดำรงตำแหน่ง</td><td>:</td><td>".$dat['pname_name']."</td></tr>";
		echo "<tr><td>ตำแหน่ง</td><td>:</td><td>".$dat['position_name']."</td></tr>";
		echo "<tr><td>วันที่เกษียณ</td><td>:</td><td>".dateThDeco($dat['ksean_date'])."</td></tr>";
		if((($dat['depart_id']==$doc['depart_run_id'])&&($_SESSION['edoc_depart_id']==$doc['depart_run_id']))||($_SESSION['edoc_write']==2)){
			echo "<tr><td colspan=\"2\"></td><td><form action=\"ksean.php\" method=\"post\">";
			echo "<input type=\"hidden\" name=\"edoc_id\" value=\"".$edoc_id."\">";
			echo "<input type=\"hidden\" name=\"ksean_id\" value=\"".$dat['ksean_id']."\">";
			echo "<input type=\"submit\" name=\"del_ksean\" value=\" ลบข้อความเกษียณ \"></form></td></tr>";
		}
		echo "</table><br /><br />\n";
	}
}

function del_ksean($ksean_id,$doc_no){
	$q = "delete from ksean where ksean_id='$ksean_id';";
	$ans = updatePG($q);
	if($ans==FALSE){
		addlog("ลบเกษียณหนังสือ","ไม่สามารถลบเกษียณหนังสือเลขที่ ".$doc_no." ได้");
		return -1;
	}	
	addlog("ลบเกษียณหนังสือ","ลบเกษียณหนังสือเลขที่ ".$doc_no." เรียบร้อยแล้ว");
	return 0;
}

function send_to_depart($edoc_id,$depart_send){
	$q = "update edoc set depart_run_id=$depart_send where edoc_id=$edoc_id;";
	$ans = updatePG($q);
	$dat = getDocInfo($edoc_id);
	if($ans==FALSE){
		addlog("เกษียณหนังสือ","ไม่สามารถส่งต่อหนังสือเลขที่ ".$dat['doc_no']." ได้");
		return -1;
	}
	if($depart_send!=0)
		addlog("เกษียณหนังสือ","ส่งต่อหนังสือเลขที่ ".$dat['doc_no']." เรียบร้อยแล้ว");
	else
		addlog("เกษียณหนังสือ","สิ้นสุดการส่งต่อหนังสือเลขที่ ".$dat['doc_no']." เรียบร้อยแล้ว");
	return 0;
}

function addSecretCon($condition,$query){
	$con = "";	
	foreach($query['secret'] as $dat){
		if($dat<=$_SESSION['edoc_secret']){
			if($con=="") $con = "(secret=$dat)";
			else $con = $con." or (secret=$dat)";
		}else{
			if($con=="") $con = "((secret=$dat) and (depart_id=".$_SESSION['edoc_depart_id']."))";
			else $con = $con." or ((secret=$dat) and (depart_id=".$_SESSION['edoc_depart_id']."))";
		}
	}
	if($con=="") return $condition;
	return $condition." (".$con.")"; 
}

function addRapidCon($condition,$query){
	$con = "";	
	foreach($query['rapid'] as $dat){
		if($con=="") $con = "(rapid=$dat)";
		else $con = $con." or (rapid=$dat)";
	}
	if($con=="") return $condition;
	return $condition." and (".$con.")"; 
}

function addDoctypeCon($condition,$query){
	if($query['doctype']==0){
		if($query['depart_id']==0) $con = "";
		else $con = " and (depart_id=".$query['depart_id'].")";
	}
	else $con = " and (edoc_type_id=".$query['doctype'].")";
	return $condition.$con;
}

function addTxtCon($condition,$txt,$column){
	$txt1 = trim($txt);
	if($txt1 == "") return $condition;
	$txt2 = explode(" ", $txt1);
	$con = "";
	foreach($txt2 as $dat){
		if($con=="") $con = "($column like '%".$dat."%')";
		else $con = $con." and ($column like '%".$dat."%')";
	}
	if($con=="") return $condition;
	return $condition." and (".$con.")"; 
}

function addDateCon($condition,$d1,$d2,$column){
	$dd1 = trim($d1);
	$dd2 = trim($d2);
	$con1 = "";
	$con2 = "";	
	
	if($dd1!=""){
		$con1 = " and ($column >= '".dateThEnco($dd1)."')";
	}
	if($dd2!=""){
		$con2 = " and ($column <= '".dateThEnco($dd2)."')";
	}
	
	return $condition.$con1.$con2;
}

function codeTime($t){
	return str_replace(":",".",$t);
}

function addDepartSend($condition,$depart,$depart_send){
	if($depart==0) return $condition;
	else if($depart==1){
		$d1 = trim($depart_send);
		$d2 = explode(" ",$d1);
		$con = "";
		foreach($d2 as $dat){
			if($con=="") $con = "(depart_send_name_out like '%".$dat."%')";
			else $con = $con." and (depart_send_name_out like '%".$dat."%')";
		}
		$con = "(".$con.")";
		$con = " and (".$con." and (depart_send_id = $depart))";
		return $condition.$con;
	}
	else{
		return $condition." and (depart_send_id = $depart)";
	}
}

?>
